{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #XOR Operation in an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: xorOperation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #数组异或操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个整数，<code>n</code> 和 <code>start</code> 。</p>\n",
    "\n",
    "<p>数组 <code>nums</code> 定义为：<code>nums[i] = start + 2*i</code>（下标从 0 开始）且 <code>n == nums.length</code> 。</p>\n",
    "\n",
    "<p>请返回 <code>nums</code> 中所有元素按位异或（<strong>XOR</strong>）后得到的结果。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 5, start = 0\n",
    "<strong>输出：</strong>8\n",
    "<strong>解释：</strong>数组 nums 为 [0, 2, 4, 6, 8]，其中 (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8 。\n",
    "     &quot;^&quot; 为按位异或 XOR 运算符。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 4, start = 3\n",
    "<strong>输出：</strong>8\n",
    "<strong>解释：</strong>数组 nums 为 [3, 5, 7, 9]，其中 (3 ^ 5 ^ 7 ^ 9) = 8.</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 1, start = 7\n",
    "<strong>输出：</strong>7\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 10, start = 5\n",
    "<strong>输出：</strong>2\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 1000</code></li>\n",
    "\t<li><code>0 &lt;= start &lt;= 1000</code></li>\n",
    "\t<li><code>n == nums.length</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [xor-operation-in-an-array](https://leetcode.cn/problems/xor-operation-in-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [xor-operation-in-an-array](https://leetcode.cn/problems/xor-operation-in-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['5\\n0', '4\\n3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        result = 0\n",
    "        for i in range(n):\n",
    "            tmp = start + 2*i\n",
    "            result = result ^ tmp\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        return start^self.xorOperation(n-1,start+2) if n else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self,n, start) -> int:\n",
    "        if n == 1:\n",
    "            return start\n",
    "        elif n > 1:\n",
    "            return start ^ self.xorOperation(n-1, start+2)\n",
    "        else:\n",
    "            return None\n",
    "\n",
    "#上面的答案有点复杂了，空间复杂度较高，可以参考下面这个：\n",
    "#class Solution:\n",
    "#    def xorOperation(self, n: int, start: int) -> int:\n",
    "#        ans = 0\n",
    "#        for i in range(n):\n",
    "#            ans ^= (start + i * 2)\n",
    " #       return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        if n == 1:\n",
    "            return start\n",
    "        \n",
    "        else:\n",
    "            return start ^ self.xorOperation(n-1, start + 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans ^=(start+2*i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums = start\n",
    "        for i in range (1, n):\n",
    "            nums = nums ^ (start + 2 * i)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans ^= (start + i * 2)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums = []\n",
    "        ret = start\n",
    "        for i in range(1, n):\n",
    "            ret = start + 2 * i ^ ret\n",
    "        return ret\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans=''\n",
    "        for i in range(n):\n",
    "            ans+=f'{start+2*i}^' if i != n-1 else f'{start+2*i}'\n",
    "        return eval(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "            res=0\n",
    "            for i in range(n):\n",
    "                res^=(start+2*i)  #逐个异或\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans ^= (start + i * 2)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans ^= start + 2 * i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        b=0\n",
    "        for i in range(n):\n",
    "            tem=start+2*i\n",
    "            b=tem^b\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums = [0 for x in range(n)]\n",
    "        for i in range(n):\n",
    "            nums[i] = start + 2*i\n",
    "        return eval(\"^\".join(str(x) for x in nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        result = 0\n",
    "        for i in range(n):\n",
    "            result ^= start + 2*i\n",
    "        return result\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(start,start+2*n,2):\n",
    "            res ^= i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans ^= start + 2*i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        xor_result = 0\n",
    "        for i in range(n):\n",
    "            xor_result ^= (start + 2*i)\n",
    "        return xor_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        res = start\n",
    "        for i in range(1, n):\n",
    "            res ^= start + 2*i\n",
    "        return res       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        for i in range(n):\n",
    "            if i == 0:\n",
    "                temp = start\n",
    "            else:\n",
    "                temp = temp ^ (start + 2 * i)\n",
    "        return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        b=0\n",
    "        for i in range(n):\n",
    "            num=start + 2*i\n",
    "            b ^= num\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        result = 0\n",
    "        for i in range(n):\n",
    "            tmp = start + 2*i\n",
    "            result = result ^ tmp \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res ^= (start + (i*2))\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        # 暴力：先求出nums， 在异或\n",
    "        # nums = [0]*n\n",
    "        # for i in range(n):\n",
    "        #     nums[i] = start + 2*i \n",
    "        # tm = 0\n",
    "        # for x in nums:\n",
    "        #     tm ^= x \n",
    "        # return tm\n",
    "\n",
    "\n",
    "        # 循环一次\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res ^= start + 2*i \n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans=start\n",
    "        for i in range(1,n):\n",
    "            num=start+2*i\n",
    "            ans^=num\n",
    "        return ans\n",
    "\n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        yihuo = 0\n",
    "        for i in range(n):\n",
    "            yihuo ^=(start+2*i)\n",
    "        return yihuo\n",
    "\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        # for i in range(n):\n",
    "        #     nums[i]=start+2*i\n",
    "        result =0\n",
    "        for i in range(n):\n",
    "            result ^= start+2*i\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, s: int) -> int:\n",
    "        rs=0\n",
    "        \n",
    "        for i in range(n):\n",
    "            rs^=2*i+s\n",
    "        return rs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        # nums=[]\n",
    "        # for i in range(n):\n",
    "        #     nums.append(start+2*i)\n",
    "        # ans=0\n",
    "        # for num in nums:\n",
    "        #     ans=ans^num\n",
    "        # return ans\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            ans=ans^(start+2*i)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums = [start + 2*i for i in range(n)]\n",
    "        x = nums[0]\n",
    "        for i in range(1, n):\n",
    "            x ^= nums[i]\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 0:\n",
    "            ans ^= start\n",
    "            start += 2\n",
    "            n -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans=[]\n",
    "        while len(ans)<n:\n",
    "            ans.append(start)\n",
    "            start+=2\n",
    "        res=reduce(lambda x,y:x^y,ans)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def xorOperation(self, n: int, start: int) -> int:\r\n",
    "        res = 0\r\n",
    "        for i in range(n):\r\n",
    "            res ^= (start + 2*i)\r\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        def sumXor(x:int) -> int:\n",
    "            if x%4 == 0:\n",
    "                return x\n",
    "            if x%4 == 1:\n",
    "                return 1\n",
    "            if x%4 == 2:\n",
    "                return x + 1\n",
    "            return 0\n",
    "        e = 1 if start%2 and n%2 else 0\n",
    "        s = start>>1\n",
    "        \n",
    "        return ((sumXor(s-1)^sumXor(s+n-1))<<1)|e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums = [start + 2 * i for i in range(n) ]\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans  = ans ^ nums[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        result = start\n",
    "        for i in range(1,n):\n",
    "            start += 2\n",
    "            result ^= start\n",
    "        \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        x=0\n",
    "        for i in range(0,n):\n",
    "            x^=start +2*i\n",
    "        return x\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "\n",
    "        for i in range(n):\n",
    "            if i == 0:\n",
    "                res = start \n",
    "            else:\n",
    "                res = res ^ start + 2 * i\n",
    "                \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans ^= (start + i * 2)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        res=0\n",
    "        for i in range(n):\n",
    "            res^=(start+i*2)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        r=start\n",
    "        for i in range(1,n):\n",
    "            r=r^(start+2*i)\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        m=start\n",
    "        for i in range(1,n):\n",
    "            m=m^(start+2*i)\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans ^= (start + i * 2)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        def sumXor(x:int) -> int:\n",
    "            if x%4 == 0:\n",
    "                return x\n",
    "            if x%4 == 1:\n",
    "                return 1\n",
    "            if x%4 == 2:\n",
    "                return x + 1\n",
    "            return 0\n",
    "        e = 1 if start%2 and n%2 else 0\n",
    "        s = start>>1\n",
    "        \n",
    "        return ((sumXor(s-1)^sumXor(s+n-1))<<1)|e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        x=0\n",
    "        for i in range(0,n):\n",
    "            x^=start +2*i\n",
    "        return x\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            ans=ans^(start+i*2)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        a=0\n",
    "        for i in range(n):\n",
    "            a=a^(start+i*2)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        res=0\n",
    "        for i in range(n):\n",
    "            res^=(start + 2*i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res ^= (start + 2*i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        a = 0\n",
    "        for i in range(n):\n",
    "            a ^= (start + i*2)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums=0\n",
    "        for i in range(n):\n",
    "            nums^=(start+i*2)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        r=start\n",
    "        for i in range(1,n):\n",
    "            r=r^(start+2*i)\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        def sumXor(x:int) -> int:\n",
    "            left = x%4\n",
    "            if left == 0:\n",
    "                return x\n",
    "            if left == 1:\n",
    "                return 1\n",
    "            if left == 2:\n",
    "                return x + 1\n",
    "            return 0\n",
    "        e = 1 if start%2 and n%2 else 0\n",
    "        s = start>>1\n",
    "        \n",
    "        return ((sumXor(s-1)^sumXor(s+n-1))<<1)|e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        \"\"\"\n",
    "        :type n: int\n",
    "        :type start: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        res = 2 * self.myxor(n, start//2)\n",
    "        if n&1 and start&1:\n",
    "            res += 1\n",
    "        return res\n",
    "\n",
    "    def myxor(self, n, start):\n",
    "        if start & 1:\n",
    "            return (start-1) ^ self.myxor(n+1, start-1)\n",
    "        else:\n",
    "            if n & 1:\n",
    "                return (n//2 & 1) ^ (start+n-1)\n",
    "            else:\n",
    "                return n//2 & 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans ^= (start + i * 2)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        x=0\n",
    "        nums=[0]*n\n",
    "        for i in range(n):\n",
    "            nums[i]=start+2*i\n",
    "            x ^=nums[i]\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        i = 0\n",
    "        num = 0\n",
    "        while i < n:\n",
    "            num ^= start + 2 *i\n",
    "            i += 1\n",
    "        return num\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums = [start + 2 * i for i in range(0, n)]\n",
    "        ans = 0\n",
    "        for i in range(0, n):\n",
    "            ans = ans ^ nums[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res ^= (start + 2 * i)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res ^= (start+i*2)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        if n > 1:\n",
    "            nums = [start + 2 * i for i in range(n)]\n",
    "            xor = nums[0] ^ nums[1]\n",
    "            for n in nums[2:]:\n",
    "                xor = xor ^ n\n",
    "            return xor\n",
    "        else:\n",
    "            return start ^ 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums = []\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            nums.append(start + 2 * i)\n",
    "        for i in nums:\n",
    "            ans ^= i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans ^= start + 2 * i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        result=0\n",
    "        for i in range(n):\n",
    "            tmp=start+2*i\n",
    "            result = result^tmp\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums=0\n",
    "        for i in range(n):\n",
    "            nums^=(start+i*2)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res ^= start + 2*i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        result = 0\n",
    "        for i in range(n):\n",
    "            result = result ^ (start + 2 * i)\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums=[]\n",
    "        m=start\n",
    "        for i in range(1,n):\n",
    "            m^=start+2*i\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        i = 0\n",
    "        nums = []\n",
    "        while i < n:\n",
    "            tag = start + 2 * i\n",
    "            nums.append(tag)\n",
    "            i += 1\n",
    "        flag = 0\n",
    "        j = 0\n",
    "        for j in range(n):\n",
    "            flag = flag ^ nums[j]\n",
    "            j += 1\n",
    "        return flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums = [start + 2 * i for i in range(0, n)]\n",
    "        ans = nums[0]\n",
    "        for i in range(1, n):\n",
    "            ans = ans ^ nums[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        k = start\n",
    "        for i in range(1,n):\n",
    "            k = k ^ (start + 2 * i)\n",
    "        return(k)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        res=start\n",
    "        for i in range(1,n):\n",
    "            res^=start+2*i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        ans = [start] \n",
    "        for i in range(n-1):\n",
    "            ans.append(ans[i]+2)\n",
    "        nans = reduce(lambda x,y: x^y,ans)\n",
    "        return nans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorOperation(self, n: int, start: int) -> int:\n",
    "        nums = [0]*n\n",
    "        for i in range(n):\n",
    "            nums[i] = start + 2*i\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ans = ans ^ nums[i]  \n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
